/**
 * 
 */
package datatype.DBtypes;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
/**
 * @author Yuval
 * 
 * 
 *         this is a class that the db should return after choosing a film in
 *         search or, this class should be created to insert new film to the
 *         db
 * 
 */
public class FilmfromDB {

	private int id = 0;
	private String name = null;

	// release date
	private int year = 0, month = 0, day = 0;



	private int value = 0;
	private String sequel = "", prequel = "";
	private int seqId, preqId;
	private String originCountry = null;
	// private genre...
	private List<Actor> actors = new ArrayList<Actor>();
	private List<Director> directors = new ArrayList<Director>();

	// we will send this list to reduce the size and overhead
	private List<Actor> newActors = new ArrayList<Actor>();
	private List<Actor> editedActors = new ArrayList<Actor>();
	private List<Director> newDirectors = new ArrayList<Director>();

	public FilmfromDB(int id, String name, int year, int month, int day,
			int value, String sequel, String prequel, String originCountry) {
		super();
		this.id = id;
		this.name = name;
		this.year = year;
		this.month = month;
		this.day = day;
		this.value = value;
		this.sequel = sequel;
		this.prequel = prequel;
		this.originCountry = originCountry;
	}

	public FilmfromDB() {
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public String getOriginCountry() {
		return originCountry;
	}

	public void setOriginCountry(String originCountry) {
		this.originCountry = originCountry;
	}

	public synchronized String getSequel() {
		return sequel;
	}

	public synchronized void setSequel(String sequel) {
		this.sequel = sequel;
	}

	public synchronized String getPrequel() {
		return prequel;
	}

	public synchronized void setPrequel(String prequel) {
		this.prequel = prequel;
	}

	public synchronized int getSeqId() {
		return seqId;
	}

	public synchronized void setSeqId(int seqId) {
		this.seqId = seqId;
	}

	public synchronized int getPreqId() {
		return preqId;
	}

	public synchronized void setPreqId(int preqId) {
		this.preqId = preqId;
	}

	public synchronized List<Actor> getActors() {
		return actors;
	}

	public synchronized void setActors(List<Actor> actors) {
		this.actors = actors;
	}

	public synchronized List<Director> getDirectors() {
		return directors;
	}

	public synchronized void setDirectors(List<Director> directors) {
		this.directors = directors;
	}

	public synchronized List<Actor> getNewActors() {
		return newActors;
	}

	public synchronized void setNewActors(List<Actor> newActors) {
		this.newActors = newActors;
	}

	public synchronized List<Director> getNewDirectors() {
		return newDirectors;
	}

	public synchronized void setNewDirectors(List<Director> newDirectors) {
		this.newDirectors = newDirectors;
	}

	public synchronized void addActor(int id, String name, String character,
			boolean isCreation) {
		if (isCreation)
			actors.add(new Actor(id, name, character));
		else
			newActors.add(new Actor(id, name, character));
	}
	
	public synchronized void editActor(int id, String name, String character) {
			editedActors.add(new Actor(id, name, character));
	}

	public synchronized void addDirector(int id, String name, boolean isCreation) {
		if (isCreation)
			directors.add(new Director(id, name));
		else
			newDirectors.add(new Director(id, name));
	}
	
	public int getActorID(int actorinList){
		if(actorinList<actors.size())
		return actors.get(actorinList).getId();
		else
			return newActors.get(actorinList-actors.size()).getId();
	}
	
	public String getActorName(int actorinList){
		if(actorinList<actors.size())
		return actors.get(actorinList).getName();
		else
			return newActors.get(actorinList-actors.size()).getName();
	}


	public void prepareActorTable(Table table) {
		for (Actor a : actors) {
			TableItem ti = new TableItem(table, SWT.NONE);
			ti.setText(0, a.getName());
			ti.setText(1, a.getCharacter());
		}
	}
	
	public String getDate(){
		String date = ""+year+"-"+ (month<10? "0"+month: ""+month) +"-"+ (day<10? "0"+day: ""+day) ;
			return date;
	}
	
	
	/* 
	 * creates the query for updating the DB
	 */
	public List<String> getQuery(int FilmID, boolean isActor){
		
		List<String> Query = new ArrayList<String>();
		int ID = id==0? FilmID: id;
		Random r = new Random(); 

		if(isActor)
		for(Actor a: newActors){
			int rand = r.nextInt();
			Query.add("Insert Into Film_Play(`Character`, Film_Id, Actor_Id, mid) Values('"+a.getCharacter()+"', "+ID+" , "+a.getId()+",'"+System.currentTimeMillis()+""+rand%999999999+"')");
		}else
			for(Director c: newDirectors){
				Query.add("Insert Into Directs(Film_Id, Director_Id) Values("+ID+" , "+c.id+")");
			}
		for(Actor a: editedActors){
			Query.add("Update Film_Play SET `Character` = '"+a.getCharacter()+"' where Actor_Id = '"+a.getId()+"'");
		}
				
		return Query;
		
	}

	@Override
	public String toString() {
		return "FilmfromDB [id=" + id + ", name=" + name + ", year=" + year
				+ ", month=" + month + ", day=" + day + ", value=" + value
				+ ", sequel=" + sequel + ", prequel=" + prequel + ", seqId="
				+ seqId + ", preqId=" + preqId + ", originCountry="
				+ originCountry + "]";
	}

	public void prepareDirectorTable(Table table) {

		for (Director d : directors) {
			TableItem ti = new TableItem(table, SWT.NONE);
			ti.setText(0, d.getName());
		}
	}

	public void setRelease(int y, int m, int d) {
		year = y;
		month = m;
		day = d;
	}

	// TODO
	public int getYear() {
		return year;
	}
	public int getMonth() {
		return month;
	}

	public int getDay() {
		return day;
	}

	private static class Actor {

		private int id = 0;
		private String name = null;
		private String character = null;

		public Actor(int id, String name, String character) {
			super();
			this.id = id;
			this.name = name;
			this.character = character;
		}

		public int getId() {
			return id;
		}

		public String getName() {
			return name;
		}

		public String getCharacter() {
			return character;
		}

	}

	private static class Director {

		private String name = null;
		private int id = 0;

		public Director(int id, String name) {
			super();
			this.name = name;
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public int getId() {
			return id;
		}
		


	}

}
